package com.begye.smartloan.apptest.renzheng.views

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.databinding.FillItemGenderBinding
import com.begye.smartloan.apptest.ext.addColor
import com.begye.smartloan.apptest.tool.addTouch
import org.json.JSONObject

class FillItemGender : LinearLayout, IFillItemResult {

    private var _binding: FillItemGenderBinding? = null
    private val binding get() = _binding!!
    private var _action: String = "GENDER"
    private var _isMust: Boolean = true

    private var _clickListener: (String, String) -> Unit = { _, _ -> }

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        context?.let {
            val layout = View.inflate(it, R.layout.fill_item_gender, this)
            _binding = FillItemGenderBinding.bind(layout)
        }
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        // Ensure binding is not null after inflation
        if (_binding == null) {
            throw IllegalStateException("ViewBinding not initialized for ${javaClass.simpleName}")
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        _binding = null  // Clear the binding reference
    }

    fun initDataSource(value: JSONObject) {
        val title = value.optString("title", "")
        val tag = value.optString("paramName", "")
        val hint = value.optString("hint", "")
        val action = value.optString("action", "")
        val isMust = value.optString("isMust", "YES")
        addTag(tag)
        addTitle(title)
        addHint(hint)
        addClickListener { _, _ -> }
        addAction(action)
        addMust(isMust)
    }

    fun addTag(value: String): FillItemGender {
        this.tag = value
        return this
    }

    fun addAction(value: String): FillItemGender {
        this._action = value
        return this
    }

    fun addMust(value: String = "YES"): FillItemGender {
        this._isMust = value == "YES"
        return this
    }

    fun addTitle(value: String): FillItemGender {
        binding.fillItemGenderTitle.text = value
        binding.fillItemGenderTitle.addColor("*", "#F55555")
        return this
    }

    fun addHint(value: String?): FillItemGender {
        return this
    }

    private fun addFemale(): FillItemGender {
        binding.fillItemGenderFemale.tag = "YES"
        binding.fillItemGenderFemale.setTextColor(Color.parseColor("#ffffff"))
        binding.fillItemGenderFemale.setBackgroundResource(R.drawable.layer_c25_000000)
        binding.fillItemGenderFemaleCheckedIcon.visibility = View.VISIBLE

        binding.fillItemGenderMale.tag = "NO"
        binding.fillItemGenderMale.setTextColor(Color.parseColor("#999999"))
        binding.fillItemGenderMale.setBackgroundResource(R.drawable.layer_c25_fafafa)
        binding.fillItemGenderMaleCheckedIcon.visibility = View.INVISIBLE
        return this
    }

    private fun addMale(): FillItemGender {
        binding.fillItemGenderMale.tag = "YES"
        binding.fillItemGenderMale.setTextColor(Color.parseColor("#ffffff"))
        binding.fillItemGenderMale.setBackgroundResource(R.drawable.layer_c25_000000)
        binding.fillItemGenderMaleCheckedIcon.visibility = View.VISIBLE

        binding.fillItemGenderFemale.tag = "NO"
        binding.fillItemGenderFemale.setTextColor(Color.parseColor("#999999"))
        binding.fillItemGenderFemale.setBackgroundResource(R.drawable.layer_c25_fafafa)
        binding.fillItemGenderFemaleCheckedIcon.visibility = View.INVISIBLE
        return this
    }

    fun addClickListener(listener: (String, String) -> Unit): FillItemGender {
        _clickListener = listener
        binding.fillItemGenderFemaleBg.setOnClickListener {
            addFemale()
            addTouch(type = "SELECT_${calTag()}", remark = "Female")
            listener.invoke(
                calTag(),
                binding.fillItemGenderFemale.text.toString().trim()
            )
        }

        binding.fillItemGenderMaleBg.setOnClickListener {
            addMale()
            addTouch(type = "SELECT_${calTag()}", remark = "Male")
            listener.invoke(
                calTag(),
                binding.fillItemGenderMale.text.toString().trim()
            )
        }
        return this
    }

    private fun calTag(): String {
        return (this.tag ?: "").toString().trim()
    }

    override fun calResult(check: Boolean): String {
        val femaleTag = (binding.fillItemGenderFemale.tag ?: "NO").toString().trim()
        if (femaleTag.contains("YES")) {
            return binding.fillItemGenderFemale.text.toString().trim()
        }

        val maleTag = (binding.fillItemGenderMale.tag ?: "NO").toString().trim()
        if (maleTag.contains("YES")) {
            return binding.fillItemGenderMale.text.toString().trim()
        }

        return ""
    }

    override fun calAction(): String {
        return this._action
    }

    override fun updateValue(fts: Long, bts: Long, index: Int, value: String) {
    }

    override fun isMust(): Boolean {
        return this._isMust
    }
}